import os




'''
AI1,AI2默认是4-20ma电流采集的;
AI3,AI4默认是0-12v电压采集的
'''

def ai_get_value(name):
    if name == 'AI1':
        try:
            raw_val = os.popen('cat /sys/devices/platform/soc/19251000.gpai/iio:device0/in_voltage2_raw')
            raw = int(raw_val.read())
            raw_val.close()
            return raw * (3000 / 4096) / 140
        except OSError:
            pass   
    if name == 'AI2':
        try:
            raw_val = os.popen('cat /sys/devices/platform/soc/19251000.gpai/iio:device0/in_voltage3_raw')
            raw = int(raw_val.read())
            raw_val.close()
            return raw * (3000 / 4096) / 140
        except OSError:
            pass 
    if name == 'AI3':
        try:
            raw_val = os.popen('cat /sys/devices/platform/soc/19251000.gpai/iio:device0/in_voltage4_raw')
            raw = int(raw_val.read())
            raw_val.close()
            return raw * (3000 / 4096) * 4
        except OSError:
            pass 
    if name == 'AI4':
        try:
            raw_val = os.popen('cat /sys/devices/platform/soc/19251000.gpai/iio:device0/in_voltage5_raw')
            raw = int(raw_val.read())
            raw_val.close()
            return raw * (3000 / 4096) * 4
        except OSError:
            pass 

def get_vbat_val():
    try:
        raw_val = os.popen('cat /sys/devices/platform/soc/19251000.gpai/iio:device0/in_voltage6_raw')
        raw = int(raw_val.read())
        print(raw)
        raw_val.close()
        return 13*raw * (3000 / 4096) #13倍分压
    except OSError:
        pass 